
import {addClaimForex, deleteClaimForex, submitClaimForex, getClaimForexList, getUnrelatorderForexList, getRelatedorderForexList, addRelateForex, cancelRelateForex, confirmfinishForex, cancelfinishForex} from '../../services/foreignExchange/chaim';
import {getStrategyList,} from '../../services/basedata/strategy';
import {getCustomerByNameOrCode} from '../../services/customer/customer';
import Constants from '../../utils/Constants';

import {parse} from 'qs';
import { message } from 'antd';
const R = require('ramda');

export default {
    namespace: 'claim',
    state: {
        page: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
        unrelatorderPage: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
        relatorderPage: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },

        list:[],
        unrelatorderList: [],
        relatorderList: [],
        strategyList:[],

        dataSource: {},
        claim: {},
        customer:{},
        status: "",
    },
    reducers: {

        //-----------------setList

        /**
         *
         * @param state
         * @param payload
         * @returns {{list, page: *}}
         */
        setlist(state, { payload }) {
            return { ...state, list: payload.list, page: R.isNil(payload.page) ? state.page : payload.page}
        },

        /**
         * 设置未关联列表
         * @param state
         * @param payload
         * @returns {{unrelatorderList, unrelatorderPage: (state.unrelatorderPage|{page, size, totalRow, totalPage})}}
         */
        setUnrelatorderlist(state, { payload }) {
            return { ...state, unrelatorderList: payload.list, unrelatorderPage: R.isNil(payload.page) ? state.unrelatorderPage : payload.page}
        },

        /**
         * 设置已关联列表
         * @param state
         * @param payload
         * @returns {{relatorderList, relatorderPage: (state.relatorderPage|{page, size, totalRow, totalPage})}}
         */
        setRelatorderlist(state, { payload }) {
            return { ...state, relatorderList: payload.list, relatorderPage: R.isNil(payload.page) ? state.relatorderPage : payload.page}
        },

        /**
         * 收汇区间
         * @param state
         * @param payload
         * @returns {{setStrategyList}}
         */
        setStrategyList(state, { payload }) {
            return { ...state, strategyList: payload.list}
        },



        //--------------------setEntity


        /**
         * 设置产品
         * @param state
         * @param action
         * @returns {{dataSource: (string|state.dataSource|{}|*)}}
         */
        setDataSource(state, action) {
            let payload = action.payload;
            return {...state, dataSource: payload.data};
        },

        /**
         * 设置客户
         * @param state
         * @param action
         * @returns {{customer: (payload.data|{}|*|string)}}
         */
        setCustomer(state, action) {
            let payload = action.payload;
            return {...state, customer: payload.data};
        },

    },
    effects: {

        /**
         * 获取分页认领
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *getClaimForexList({payload}, { call, put, select }) {
            let page = yield select(state => state.claim.page);
            page = R.isNil(payload) || R.isNil(payload.page) ? page : payload.page;

            const res = yield call(getClaimForexList, {
                forexId: R.isNil(payload) || R.isNil(payload.forexId) ? "" : payload.forexId,
                page
            });

            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'setlist',
                    payload: {
                        list: R.isNil(content.list) ? [] : content.list,
                        page : R.isNil(content.page) ? Constants.DEFAULTPAGE : content.page,
                        status: status
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 认领外汇
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *addClaimForex({ payload }, { call, put, select }) {
            const res = yield call(addClaimForex, payload);
            if (res.data.resultCode === "ok") {
                message.info("认领成功！");

                if(payload.onSuccess)payload.onSuccess();

            } else {
                message.error(res.data.errMessage);
                if(payload.onFail)payload.onFail();
            }
        },

        /**
         * 删除认领记录
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *deleteClaimForex({ payload }, { call, put, select }) {
            let page = yield select(state => state.claim.page);
            const res = yield call(deleteClaimForex, {forexId : payload.forexId, claimId : payload.claimId });
            if (res.data.resultCode === "ok") {
                message.info("删除成功！");

                if(payload.onSuccess)payload.onSuccess();

                yield put({
                    type: "getClaimForexList",
                    payload: {
                        page: page,
                        forexId: payload.forexId
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 提交认领
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *submitClaimForex({ payload }, { call, put, select }) {
            const res = yield call(submitClaimForex, {forexId : payload.forexId});
            if (res.data.resultCode === "ok") {
                let content = res.data.content

                message.info("提交认领记录成功！");

                if(payload.onSuccess)payload.onSuccess();

            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 关联订单
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *addRelateForex({ payload }, { call, put, select }) {
            const page = yield select(state => state.claim.relatorderPage);
            const unrelatorderPage = yield select(state => state.claim.unrelatorderPage);

            const res = yield call(addRelateForex, payload);
            if (res.data.resultCode === "ok") {

                let content = res.data.content

                message.info("关联成功！");

                if(payload.onSuccess)payload.onSuccess();

                yield put({
                    type: 'getRelatedorderForexList',
                    payload: {
                        id: payload.forxid,
                        page : page

                    }
                });

                yield put({
                    type: 'getUnrelatorderForexList',
                    payload: {
                        id: payload.forxid,
                        page : unrelatorderPage

                    }
                });

            } else {
                if(payload.onFail)payload.onFail();
                message.error(res.data.errMessage);
            }
        },



        /**
         * 取消关联成功
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *cancelRelateForex({ payload }, { call, put, select }) {
            const page = yield select(state => state.claim.relatorderPage);
            const unrelatorderPage = yield select(state => state.claim.unrelatorderPage);

            const res = yield call(cancelRelateForex, payload);

            if (res.data.resultCode === "ok") {

                let content = res.data.content

                message.info("取消关联成功！");

                if(payload.onSuccess)payload.onSuccess();

                yield put({
                    type: 'getRelatedorderForexList',
                    payload: {
                        id: payload.forxid,
                        page : page

                    }
                });

                yield put({
                    type: 'getUnrelatorderForexList',
                    payload: {
                        id: payload.forxid,
                        page : unrelatorderPage

                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 确认外汇收齐
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *confirmfinishForex({ payload }, { call, put, select }) {
            const page = yield select(state => state.claim.relatorderPage);
            const unrelatorderPage = yield select(state => state.claim.unrelatorderPage);

            const res = yield call(confirmfinishForex, {orderId: payload.orderid,});

            if (res.data.resultCode === "ok") {
                let content = res.data.content
                message.info(" 确认收汇成功！");

                yield put({
                    type: 'getRelatedorderForexList',
                    payload: {
                        id: payload.forxid,
                        page : page

                    }
                });

                yield put({
                    type: 'getUnrelatorderForexList',
                    payload: {
                        id: payload.forxid,
                        page : unrelatorderPage

                    }
                });

            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 确认外汇收齐
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *cancelfinishForex({ payload }, { call, put, select }) {
            const page = yield select(state => state.claim.relatorderPage);
            const unrelatorderPage = yield select(state => state.claim.unrelatorderPage);

            const res = yield call(cancelfinishForex, {...payload});

            if (res.data.resultCode === "ok") {
                message.info(" 取消订单外汇收齐成功！");

                yield put({
                    type: 'getRelatedorderForexList',
                    payload: {
                        id: payload.forxid,
                        page : page

                    }
                });

                yield put({
                    type: 'getUnrelatorderForexList',
                    payload: {
                        id: payload.forxid,
                        page : unrelatorderPage

                    }
                });

            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 查询未关联外汇
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *getUnrelatorderForexList({payload}, { call, put, select }) {
            const page = yield select(state => state.claim.unrelatorderPage);

            const res = yield call(getUnrelatorderForexList, {id: payload.id, page});

            if (res.data.resultCode === "ok") {
                let content = res.data.content;

                let list = R.isNil(content.list) ? [] : content.list;

                if(!R.isEmpty(list)){
                    for(let i=0; i<list.length; i++){
                        list[i].amount = 0;
                    }
                }

                yield put({
                    type: 'setUnrelatorderlist',
                    payload: {
                        list: R.isNil(content.list) ? [] : content.list,
                        page : R.isNil(content.page) ? Constants.DEFAULTPAGE : content.page,

                    }
                });
            }else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 查询关联外汇
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *getRelatedorderForexList({payload}, { call, put, select }) {
            const page = yield select(state => state.claim.relatorderPage);

            const res = yield call(getRelatedorderForexList, {id: payload.id, page});

            if (res.data.resultCode === "ok") {
                let content = res.data.content;

                let form  = payload.form;

                yield put({
                    type: 'setRelatorderlist',
                    payload: {
                        list: R.isNil(content.list) ? [] : content.list,
                        page : R.isNil(content.page) ? Constants.DEFAULTPAGE : content.page,

                    }
                });
            }else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 获取收汇区间
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *getStrategyList({payload}, { call, put, select }) {
            let page = {page: 1, size: 100, totalRow: 0, totalPage: 0}
            const res = yield call(getStrategyList, {businesstype: "orderSettlement", status : "1", page});
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'setStrategyList',
                    payload: {
                        list: R.isNil(content.list) ? [] : content.list,
                    }
                });
            }
        },


        /**
         * 查询客户
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *getCustomerByNameOrCode({payload}, {call, put, select}) {
            const res = yield call(getCustomerByNameOrCode, {nameOrCode: payload.nameOrCode});

            if (res.data.resultCode === "ok") {
                let data = res.data;
                    payload.onEmpty();
                yield put({
                    type: "setCustomer",
                    payload: {
                        data: data,
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }

        },

        *resetAmoumt({payload}, { call, put, select }) {
            const unrelatorderList = yield select(state => state.claim.unrelatorderList);

            if(!R.isNil(unrelatorderList) && !R.isEmpty(unrelatorderList)){
                for(let i=0; i< unrelatorderList.length; i++){
                    unrelatorderList[i].amount= ""
                }
                yield put({
                    type: 'setUnrelatorderlist',
                    payload: {
                        list: unrelatorderList,

                    }
                });

            }



        },

    },
    subscriptions: {
        setup({ history, dispatch }) {
            return history.listen(({ pathname }) => {

            });
        }
    }
};